import 'package:flutter/widgets.dart';
import 'dart:math';
import 'dart:ui';
import 'package:flutter/material.dart';

class RectBgWidget extends CustomPainter {
  bool empty;
  int radius;
  Color color;
  Color borderColor;
  int width;
  int height;
  RectBgWidget({this.empty = false, this.radius = 0, this.color = Colors.white, this.borderColor = Colors.black38, this.width = 0, this.height = 0});



  @override
  void paint(Canvas canvas, Size size) {
    drawPiePath(canvas, size);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }

  /// 绘制饼图的每块区域 和文本
  drawPiePath(Canvas canvas, Size size) {
    Paint mPaint = Paint();
    mPaint.style = PaintingStyle.fill;
    mPaint.strokeWidth = 5;
    mPaint.strokeCap = StrokeCap.round;

    double halfWidth = size.width/2;
    double halfHeight = size.height/2;
    if(width != 0 && height != 0){
      halfWidth = width/2;
      halfHeight = height/2;
    } else if(width != 0){
      height = width;
      halfWidth = width/2;
      halfHeight = height/2;
    } else {
      halfWidth = size.width/2;
      halfHeight = size.height/2;
    }
    Offset center = Offset(halfWidth, halfHeight);  //  坐标中心
    double radius = min(halfWidth, halfHeight);  //  半径

//    mPaint.color = color;
//    canvas.drawOval(Rect.fromCircle(center: center, radius: radius), mPaint);
    if(empty){
      radius = min(halfWidth-1, halfHeight-1);  //  半径
      mPaint.color = color;
//      canvas.drawOval(Rect.fromCircle(center: center, radius: radius), mPaint);

      mPaint.color = borderColor;
      center = Offset(halfWidth, halfWidth);
      canvas.drawOval(Rect.fromCircle(center: center, radius: radius), mPaint);
    } else {
      radius = min(halfWidth, halfHeight);  //  半径
      mPaint.color = color;
      canvas.drawOval(Rect.fromCircle(center: center, radius: radius), mPaint);
    }
  }


}